angular.module("UranusWei").factory('wxshare', ["commonFactory", "appconfig", function (commonFactory, appconfig) {
    var initWx = {
        share: {
            title: appconfig.title, // 分享标题
            desc: appconfig.desc,
            link: appconfig.shareurl, // 分享链接
            imgUrl: appconfig.sharelogo, // 分享图标
            success: function () {
                // 用户确认分享后执行的回调函数
            },
            cancel: function () {
                // 用户取消分享后执行的回调函数 
            }
        },
        wxconfig: {
            checkJsApi: false,
            onMenuShareTimeline: false,
            onMenuShareAppMessage: false,
            onMenuShareQQ: false,
            onMenuShareWeibo: false,
            // onMenuShareQZone: false,
            hideOptionMenu: false,
            showOptionMenu: false,
            closeWindow: false,
            hideMenuItems: false,
            chooseImage: false,
            uploadImage: false
        },
        init: function () {
            var getsharecode, fullname;
            commonFactory.ajax("/api/user/GetInvitationCode", {}, function (result) {//获取邀请码
                if (result.code == 1) {
                    getsharecode = result.data.invitationCode;
                } else if (result.code == 8) {
                    commonFactory.loginout()
                }
                commonFactory.ajax("/api/share/SaveShareLog", { type: '1' }, function (result) {//点击就统计
                }, function () {
                }, "post");
            }, function () {

            }, "get");
            commonFactory.ajax("/api/user/GetUserInfo", {}, function (result) {
                if (result.code == 1) {
                    fullname = result.data.fullName;
                }
            }, function () {

            }, "post");
            /*微信认证*/
            var globalrooturl = location.href.split('#')[0] || location.href;
            commonFactory.ajax("https://core.youlijinfu.com/ticket", {}, function (result) {
                // commonFactory.ajax("http://app.test.youlijinfu.cn/wxJssdk/getJssdk?r=" + commonFactory.GetRandomNum(10, 100), { ticket: result, url: globalrooturl }, function (config) {
                commonFactory.ajax("https://wei.youlijinfu.com/wxJssdk/getJssdk?r=" + commonFactory.GetRandomNum(10, 100), { ticket: result, url: globalrooturl }, function (config) {
                    var configData = config;
                    wx.config({
                        debug: false,
                        appId: 'wx2fd02548c090566a',
                        timestamp: configData.timestamp,
                        nonceStr: configData.nonceStr,
                        signature: configData.signature,
                        jsApiList: [
                            'checkJsApi',
                            'onMenuShareTimeline',
                            'onMenuShareAppMessage',
                            'onMenuShareQQ',
                            'onMenuShareWeibo',
                            // 'onMenuShareQZone',
                            'hideOptionMenu',
                            'showOptionMenu',
                            'closeWindow',
                            'hideMenuItems',
                            'chooseImage',
                            'uploadImage'
                        ]
                    });
                    //通过ready接口处理成功验证
                    wx.ready(function () {
                        //检查是否支持接口
                        wx.checkJsApi({
                            jsApiList: [
                                'onMenuShareTimeline', 'onMenuShareAppMessage', 'onMenuShareWeibo', 'onMenuShareQQ',
                                // 'onMenuShareQZone',
                                'hideOptionMenu', 'showOptionMenu', 'closeWindow', 'hideMenuItems', 'chooseImage', 'uploadImage'], // 需要检测的JS接口列表，所有JS接口列表见附录2,
                            success: function (res) {
                                initWx.wxconfig.onMenuShareTimeline = res.checkResult.onMenuShareTimeline;
                                initWx.wxconfig.onMenuShareAppMessage = res.checkResult.onMenuShareAppMessage;
                                initWx.wxconfig.onMenuShareQQ = res.checkResult.onMenuShareQQ;
                                initWx.wxconfig.onMenuShareWeibo = res.checkResult.onMenuShareWeibo;
                                // wxconfig.onMenuShareQZone = res.checkResult.onMenuShareQZone;
                                initWx.wxconfig.hideOptionMenu = res.checkResult.hideOptionMenu;
                                initWx.wxconfig.showOptionMenu = res.checkResult.showOptionMenu;
                                initWx.wxconfig.closeWindow = res.checkResult.closeWindow;
                                initWx.wxconfig.hideMenuItems = res.checkResult.hideMenuItems;
                                initWx.wxconfig.chooseImage = res.checkResult.chooseImage;
                                initWx.wxconfig.uploadImage = res.checkResult.uploadImage;
                            }
                        });
                        initWx.share.title = fullname + '一直在这里理财，现在邀请您一起赚钱！';
                        initWx.share.desc = '这里有11888元体验金和' + fullname + '送您的200元现金礼包，快来领吧！'
                        initWx.share.link = appconfig.shareurl + getsharecode 
                        //分享到朋友圈
                        wx.onMenuShareTimeline({
                            title: initWx.share.title,   // 分享标题
                            desc: initWx.share.desc,    // 分享描述
                            link: initWx.share.link,     // 分享链接
                            imgUrl: initWx.share.imgUrl,   // 分享图标
                            success: initWx.share.success,
                            cancel: initWx.share.cancel
                        });
                        //分享给朋友
                        wx.onMenuShareAppMessage({
                            title: initWx.share.title,  // 分享标题
                            desc: initWx.share.desc,    // 分享描述
                            link: initWx.share.link,    // 分享链接
                            imgUrl: initWx.share.imgUrl,  // 分享图标
                            type: "link",       // 分享类型,music、video或link，不填默认为link
                            dataUrl: "",        // 如果type是music或video，则要提供数据链接，默认为空
                            success: initWx.share.success,
                            cancel: initWx.share.cancel
                        });
                        //分享到QQ
                        wx.onMenuShareQQ({
                            title: initWx.share.title,  // 分享标题
                            desc: initWx.share.desc,    // 分享描述
                            link: initWx.share.link,    // 分享链接
                            imgUrl: initWx.share.imgUrl,  // 分享图标
                            success: initWx.share.success,
                            cancel: initWx.share.cancel
                        });
                        //分享到腾讯微博
                        // wx.onMenuShareWeibo({
                        //     title: initWx.share.title,  // 分享标题
                        //     desc: initWx.share.desc,    // 分享描述
                        //     link: initWx.share.link,    // 分享链接
                        //     imgUrl: initWx.share.imgUrl,  // 分享图标
                        //     success: initWx.share.success,
                        //     cancel: initWx.share.cancel
                        // });
                        //分享到qq空间
                        // wx.onMenuShareQZone({
                        // 	title: _wxconfig.share.title,  // 分享标题
                        //     desc: _wxconfig.share.desc,    // 分享描述
                        //     link: _wxconfig.share.link,    // 分享链接
                        //     imgUrl: _wxconfig.share.imgUrl,  // 分享图标
                        // 	success: _wxconfig.share.success,
                        //     cancel: _wxconfig.share.cancel
                        // }); 
                        wx.hideMenuItems({
                            menuList: ['menuItem:editTag', 'menuItem:delete', 'menuItem:copyUrl', 'menuItem:originPag', 'menuItem:readMode', 'menuItem:openWithQQBrowser', 'menuItem:openWithSafari', 'menuItem:share:email', 'menuItem:share:brand', 'menuItem:favorite', 'menuItem:share:QZone', 'menuItem:share:weiboApp','menuItem:share:facebook']//, 'menuItem:share:qq'
                        });
                        if (typeof initWx.wxonload == 'function') {
                            initWx.wxonload();
                        }
                    });
                    //通过error接口处理失败验证
                    wx.error(function (res) {
                        //alert("信息验证失败,请重新打开页面");
                        // console.log(res);
                    });
                }, function () {

                }, "post");
            }, function () {

            }, "get");
        },
        initshare: function () {
            if (!initWx.wxconfig.onMenuShareTimeline) {
                return;
            }
            wx.onMenuShareTimeline({
                title: initWx.share.title,   // 分享标题
                desc: initWx.share.desc,    // 分享描述
                link: initWx.share.link,     // 分享链接
                imgUrl: initWx.share.imgUrl,   // 分享图标
                success: initWx.share.success,
                cancel: initWx.share.cancel
            });
            //分享给朋友
            wx.onMenuShareAppMessage({
                title: initWx.share.title,  // 分享标题
                desc: initWx.share.desc,    // 分享描述
                link: initWx.share.link,    // 分享链接
                imgUrl: initWx.share.imgUrl,  // 分享图标
                type: "link",       // 分享类型,music、video或link，不填默认为link
                dataUrl: "",        // 如果type是music或video，则要提供数据链接，默认为空
                success: initWx.share.success,
                cancel: initWx.share.cancel
            });
            //分享到QQ
            wx.onMenuShareQQ({
                title: initWx.share.title,  // 分享标题
                desc: initWx.share.desc,    // 分享描述
                link: initWx.share.link,    // 分享链接
                imgUrl: initWx.share.imgUrl,  // 分享图标
                success: initWx.share.success,
                cancel: initWx.share.cancel
            });
            //分享到腾讯微博
            // wx.onMenuShareWeibo({
            //     title: initWx.share.title,  // 分享标题
            //     desc: initWx.share.desc,    // 分享描述
            //     link: initWx.share.link,    // 分享链接
            //     imgUrl: initWx.share.imgUrl,  // 分享图标
            //     success: initWx.share.success,
            //     cancel: initWx.share.cancel
            // });
        }
    }
    return initWx;
}]);